package indi.wshape1.takeawaydeliveryinfo.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Wshape1
 * @version 1.0
 * @description 拦截直接访问.html .htm格式文件 抛出404
 * @since 2023-01-18
 */

public class DirectAccessInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getDispatcherType() == DispatcherType.REQUEST) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            request.getRequestDispatcher("404").forward(request, response);
            return false;
        }
        return true;
    }
}
